﻿#region Copyright(C) 2018 FI Ltd. All rights reserved.
// ==================================================================================================
// Copyright(C) 2018 FI Ltd. All rights reserved.
//
// SYSTEM NAME	    : FI.Net Application Framework
// COMPONENT ID	    : FI.Core
// COMPONENT DESC   : FI.Net Core Foundation
//
// CREATED DATE/BY  : 18 Aug 2018 / Tian Mi
//
// REVISION HISTORY :	
// DATE/BY			SR#/CS/PM#/OTHERS		DESCRIPTION OF CHANGE
// ==================================================================================================
#endregion

using FI.Core.Utilities;
namespace FI.Core.Caching
{
    /// <summary>
    /// The base class of all concrete cache store classes.
    /// </summary>
    public abstract class CacheStoreBase : ICacheStore
    {
        #region Properties
        /// <summary>
        /// Gets a value indicating whether this <see cref="ICacheStore" /> is enabled.
        /// </summary>
        /// <value>
        ///   <c>true</c> if enabled; otherwise, <c>false</c>.
        /// </value>
        public virtual bool Enabled
        {
            get { return true; }
        }
        #endregion

        #region Protected Methods
        /// <summary>
        /// Adds the specified value into cache.
        /// </summary>
        /// <param name="key">The key of the cache item to be added.</param>
        /// <param name="value">The value of the cache item to be added.</param>
        protected abstract void AddCore(string key, object value);

        /// <summary>
        /// Adds or replaces an object in the cache.
        /// </summary>
        /// <param name="key">The unique value that is used to identify the object in the cache.</param>
        /// <param name="value">The object to add or replace.</param>
        protected abstract void PutCore(string key, object value);

        /// <summary>
        /// Remove the specified cache item.
        /// </summary>
        /// <param name="key">The key of the cache item to be deleted.</param>
        protected abstract void RemoveCore(string key);

        /// <summary>
        /// Tries the get value of the cache item based on the given key.
        /// </summary>
        /// <param name="key">The key of the cache item.</param>
        /// <param name="value">The value of the cache item.</param>
        /// <returns>
        /// A <see cref="bool" /> value indicating whether to get the value from cache store.
        /// </returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1007:UseGenericsWhereAppropriate")]
        protected abstract bool TryGetDataCore(string key, out object value);

        /// <summary>
        /// Remove all cache items added by the cache store itself.
        /// </summary>
        protected abstract void ClearCore();

        #endregion

        #region Public Methods
        /// <summary>
        /// Adds the specified value into cache.
        /// </summary>
        /// <remarks>Nothing will be done if not enabled.</remarks>
        /// <param name="key">The key of the cache item to be added.</param>
        /// <param name="value">The value of the cache item to be added.</param>
        public void Add(string key, object value)
        {
            Guard.ArgumentNotNullOrEmpty(key, "key");
            if (this.Enabled)
            {
                this.AddCore(key, value);
            }
        }

        /// <summary>
        /// Adds or replaces an object in the cache.
        /// </summary>
        /// <param name="key">The unique value that is used to identify the object in the cache.</param>
        /// <param name="value">The object to add or replace.</param>
        public void Put(string key, object value)
        {
            Guard.ArgumentNotNullOrEmpty(key, "key");
            if (this.Enabled)
            {
                this.PutCore(key, value);
            }
        }

        /// <summary>
        /// Remove specified cahce item.
        /// </summary>
        /// <param name="key">The key of cache item to be deleted.</param>
        public void Remove(string key)
        {
            Guard.ArgumentNotNullOrEmpty(key, "key");
            if (this.Enabled)
            {
                this.RemoveCore(key);
            }
        }

        /// <summary>
        /// Tries the get value of the cache item based on the given key.
        /// </summary>
        /// <remarks>This method will directly return false if not enabled.</remarks>
        /// <param name="key">The key of the cache item.</param>
        /// <param name="value">The value of the cache item.</param>
        /// <returns>
        /// A <see cref="bool" /> value indicating whether to get the value from cache store.
        /// </returns>
        public bool TryGetData(string key, out object value)
        {
            Guard.ArgumentNotNullOrEmpty(key, "key");
            if (this.Enabled)
            {
                return this.TryGetDataCore(key, out value);
            }
            value = null;
            return false;
        }

        /// <summary>
        /// Remove all cache items added by the cache store itself.
        /// </summary>
        /// <remarks>Nothing will be done if not enabled.</remarks>
        public void Clear()
        {
            if (this.Enabled)
            {
                this.ClearCore();
            }
        }

        /// <summary>
        /// Tries the get value of the cache item based on the given key.
        /// </summary>
        /// <typeparam name="T">The type of the cache item's value.</typeparam>
        /// <param name="key">The key of the cache item.</param>
        /// <param name="value">The value of the cache item.</param>
        /// <returns>
        /// A <see cref="bool" /> value indicating whether to get the value from cache store.
        /// </returns>
        public bool TryGetData<T>(string key, out T value)
        {
            Guard.ArgumentNotNullOrEmpty(key, "key");
            object obj;
            if (this.TryGetData(key, out obj))
            {
                if (null == obj)
                {
                    value = default(T);
                }
                else
                {
                    value = (T)obj;
                }
                return true;
            }
            value = default(T);
            return false;
        }

        #endregion
    }
}